<?php

class IndexAction extends BaseAction
{
    //前置方法
    Public function _initialize()
    {

    }

    public function site()
    {
        $tpl = $_GET['tpl'];
        $theme = C('SYS_DEFAULT_THEME');
        $this->display($theme . ':index_' . $tpl);
    }

    public function index()
    {
        $theme = C('SYS_DEFAULT_THEME');


        //定义seo相关内容
        $mtitle = C('JL_WEBNAME');
        $mdesc = C('JL_DESC');
        $mkey = C('JL_KEYWORDS');
        $mrights = C('JL_POWERBY');

        $this->assign('jl_title', $mtitle);
        $this->assign('jl_desc', $mdesc);
        $this->assign('jl_key', $mkey);
        $this->assign('jl_rights', $mrights);
        $this->assign("defaultimg", C('SYS_DEFAULT_IMG'));

        if (C('JL_HTML_CACHE')) {
            redirect(C('JL_CMSPATH') . '/index.html');
        } else {
            $this->display($theme . ':index');
        }
    }

    public function teacher(){
        $theme = C('SYS_DEFAULT_THEME');

        if($_SESSION['teacher'] == ''){
            $this->error('请登录您的教职员工账号',U('Index/show',array('tid'=>40)));
        }


        //定义seo相关内容
        $mtitle = C('JL_WEBNAME');
        $mdesc = C('JL_DESC');
        $mkey = C('JL_KEYWORDS');
        $mrights = C('JL_POWERBY');

        $this->assign('jl_title', '教职员工-'.$mtitle);
        $this->assign('jl_desc', $mdesc);
        $this->assign('jl_key', $mkey);
        $this->assign('jl_rights', $mrights);
        $this->assign("defaultimg", C('SYS_DEFAULT_IMG'));

        if (C('JL_HTML_CACHE')) {
            redirect(C('JL_CMSPATH') . '/index.html');
        } else {
            $this->display($theme . ':teacher');
        }
    }


    public function show()
    {
        $tid = $this->_get('tid');
        if ($tid === null) {
            halt("获取栏目id错误！");
        }

        /*if($tid == 36 && $_SESSION['teacher'] == ''){
            $this->error('请登录您的教职员工账号',U('Index/show',array('tid'=>40)));
        }*/

        if($tid == 40 && $_SESSION['teacher'] != ''){
            header("location:".U('Index/teacher')."");
        }

        $m = new ArctypeModel();
        $sid = getSiteId();
        $map['siteid'] = array("eq", $sid);
        $map['id'] = array("eq", $tid);
        $arctypeArr = $m->where($map)->find();

        //父级栏目的名字
        $fname = $m->where("id={$arctypeArr['fid']}")->getField('name');
        $this->assign("fname",$fname);

        //当访问的栏目不存时，跳转
        if (empty($arctypeArr)) {
            $this->error("404 该页面不存在！");
            return;
        }

        //模板查找开始
        $tdir = $arctypeArr['tdir'];
        if (empty($tdir) || !is_file($tdir)) {
            $tdir = $m->getDir($tid, $arctypeArr['channel'], 'tdir');
        }
        if (empty($tdir) || !is_file($tdir)) {
            halt($tdir . "栏目模板不存在！");
        }
        //模板查找结束
        //todo 单页模型type ==3
        if ($arctypeArr['type'] == 3) {
            $m3 = new ChannelModel();
            $channelInfo = $m3->getOne($arctypeArr['channel']);
            $table = $channelInfo['addtable'];
            if (empty($table)) {
                halt("未知的内容模型！");
                return;
            }
            $m2 = M($table);
            $pageArr = $m2->where("typeid = " . $arctypeArr['id'])->find();
            if (!empty($pageArr)) {
                $arctypeArr = $arctypeArr + $pageArr;
            }

            if (isIntranet()) {
                $m->where("typeid = " . $arctypeArr['id'])->setInc('click2');
            } else {
                $m->where("typeid = " . $arctypeArr['id'])->setInc('click');
            }
        } else {
            if (isIntranet()) {
                $m->where("id=$tid")->setInc('click2');
            } else {
                $m->where("id=$tid")->setInc('click');
            }
        }

        //定义seo相关内容
        if (empty($arctypeArr['mtitle'])) {
            $mtitle = $arctypeArr['name'] . "-" . C('JL_WEBNAME');
        } else {
            $mtitle = $arctypeArr['mtitle'];
        }

        if (empty($arctypeArr['mdesc'])) {
            $mdesc = C('JL_DESC');
        } else {
            $mdesc = $arctypeArr['mdesc'];
        }

        if (empty($arctypeArr['mkey'])) {
            $mkey = C('JL_KEYWORDS');
        } else {
            $mkey = $arctypeArr['mkey'];
        }

        $this->assign('jl_title', $mtitle);
        $this->assign('jl_desc', $mdesc);
        $this->assign('jl_key', $mkey);
        $this->assign('arctype', $arctypeArr);
        $this->assign('tid', $tid);
        $this->assign('position', $m->getRouteLine($tid));
        $this->assign("defaultimg", C('SYS_DEFAULT_IMG'));
        $this->display($tdir);
    }

    public function view()
    {
        $aid = $this->_get('aid');
        if (empty($aid)) {
            $this->error("404 该页面不存在！");
            return;
        }
        $sid = getSiteId();
        $m = new ArchivesModel();
        $m2 = new ArctypeModel();
        $m3 = new ChannelModel();

        $archivesArr = $m->where('id=' . $aid)->find();
        if (empty($archivesArr)) {
            $this->error("404 页面不存在！");
            return;
        }

        $tid = $archivesArr['typeid'];
        $map = array();
        $map['siteid'] = array("eq", $sid);
        $map['id'] = array("eq", $tid);
        $arctypeArr = $m2->where($map)->find();

        //父级栏目的名字
        $fname = $m2->where("id={$arctypeArr['fid']}")->getField('name');
        $this->assign("fname",$fname);


        //如果访问的页面不存在，则调回首页
        if (empty($arctypeArr)) {
            $this->error("404 页面不存在！");
            return;
        }

        //获取内容模板开始
        $cdir = $archivesArr['cdir'];
        if (empty($cdir) || !is_file($cdir)) {
            $cdir = $arctypeArr['cdir'];
            if (empty($cdir) || !is_file($cdir)) {
                $cdir = $m2->getDir($tid, $archivesArr['channel'], 'cdir');
            }
        }

        if (empty($cdir) || !is_file($cdir)) {
            halt($cdir . "内容模板不存在！");
            return;
        }
        //获取内容模板结束

        $channelArr = $m3->getOne($arctypeArr['channel']);
        if (empty($channelArr)) {
            halt("未知的内容模型!");
            return;
        }

        $addtable = $channelArr['addtable'];

        $m->_link['addfields']['class_name'] = $addtable;
        $data = $m->relation(true)->where('id=' . $aid)->find();

        foreach ($data['addfields'] as $k => $v) {
            if (in_array($k, C('SYS_PAGE_BREAK_FIELDS'))) $v = pageBreak($v);
            $data[$k] = $v;
        }

        /*以下内容为分页、访问权限控制，有需求才打开
        //获取阅读权限
        $rank = $data['rank'];
        if(empty($rank) || $rank == '缺省' ){
            $rank = $arctypeArr['rank'];
            if(empty($rank)){
                $rank = $m2->getParentDir($tid,'rank');
            }
        }

        //浏览权限控制内容
        if( empty($_SESSION[C('MEMBER_AUTH_KEY')]) && !empty($rank) && $rank == '会员开放'){
            $data['txt'] =msubstr(strip_tags($data['txt']),0,380);
            $this->assign("rank",1);
        }
        */

        //点击数累加
        if (isIntranet()) {
            //内网
            $m->where("id = $aid")->setInc('click2');
        } else {
            $m->where("id = $aid")->setInc('click');
        }

        //定义seo相关内容
        $mtitle = $archivesArr['title']."-".$arctypeArr['name']."-".C('JL_WEBNAME');

        if (!empty($archivesArr['desc'])) {
            $mdesc = $archivesArr['desc'];
        } else {
            $mdesc = C('JL_DESC');
        }

        if (!empty($archivesArr['keywords'])) {
            $mkey = $archivesArr['keywords'];
        } else {
            $mkey = C('JL_KEYWORDS');
        }

        /*以下为查找相关内容、文章评论。有需要才打开
         //获取相关阅读
         $map = array();
         $map['id'] = array('neq',$aid);
         $map['typeid'] = array('eq',$tid);
         $moreArcs =$m->where($map)->limit("0,8")->select();
         $this->assign('moreArcs',$moreArcs);
         //评论
         $m5 = M('archivesFeedback');
         $feedBackArr = $m5->where("aid = $aid and status = 2")->select();
         $this->assign('feedBackArr',$feedBackArr);
         */

        //上一篇
        $prev = $m->where("id>{$aid} AND status not in(-1,0) AND typeid={$tid}")->order("id ASC")->find();
        if (!$prev) {
            $this->assign("prev", '<a href="javascript:void(0)">没有了</a>');
        } else {
            if(strstr($prev['flag'], 'j')){
                $this->assign("prev", "<a href='" . $prev['desc'] . "' target='_blank' title='" . $prev['title'] . "'>上一篇</a>");
            }else{
                $this->assign("prev", "<a href='" . U("/index/view/aid/{$prev['id']}") . "' title='" . $prev['title'] . "'>上一篇</a>");
            }
        }

        //下一篇
        $next = $m->where("id<{$aid} AND status not in(-1,0) AND typeid={$tid}")->order("id DESC")->find();
        if (!$next) {
            $this->assign("next", '<a href="javascript:void(0)">没有了</a>');
        } else {
            if(strstr($next['flag'], 'j')){
                $this->assign("next", "<a href='" . $next['desc'] . "' title='" . $next['title'] . "'>下一篇</a>");
            }else{
                $this->assign("next", "<a href='" . U("/index/view/aid/{$next['id']}") . "' title='" . $next['title'] . "'>下一篇</a>");
            }
        }

        //返回列表
        $back = "<a href='" . U("/index/show/tid/{$tid}") . "'>返回</a>";
        $this->assign("back", $back);

        $this->assign('jl_title', $mtitle);
        $this->assign('jl_desc', $mdesc);
        $this->assign('jl_key', $mkey);
        $this->assign("defaultimg", C('SYS_DEFAULT_IMG'));
        $this->assign('c', $data);
        $this->assign('tid', $tid);
        $this->assign('arctype', $arctypeArr);
        $this->assign('position', $m2->getRouteLine($tid));
        $this->display($cdir);
    }


    //不含主表的系统模型
    public function extview()
    {
        $aid = $this->_get('aid');
        $cid = $this->_get('cid');
        if ($aid === null || $cid === null) {
            $this->error("404 页面不存在！");
            return;
        }
        $m2 = new ArctypeModel();
        $m3 = new ChannelModel();
        $sid = getSiteId();
        $channelArr = $m3->getOne($cid);
        $table = $channelArr['addtable'];

        //如果是外部模型
        if ($channelArr['issystem'] == 2) {
            $this->_extview($aid, $table);
            return;
        }

        $m4 = M($table);
        $archivesArr = $m4->where('id=' . $aid)->find();
        if (empty($archivesArr)) {
            $this->error("404 页面不存在！");
            return;
        } else {
            if (isIntranet()) {
                //内网
                $m4->where("id = $aid")->setInc('click2');
            } else {
                $m4->where("id = $aid")->setInc('click');
            }
        }

        $tid = $archivesArr['typeid'];
        $map = array();
        $map['id'] = array("eq", $tid);
        $map['siteid'] = array("eq", $sid);
        $arctypeArr = $m2->where($map)->find();
        if (empty($arctypeArr)) {
            $this->error("404 页面不存在！");
            return;
        }

        //获取内容模板
       /* $cdir = $archivesArr['cdir'];
        if (empty($cdir) || !is_file($cdir)) {
            $cdir = $arctypeArr['cdir'];
            if (empty($cdir) || !is_file($cdir)) {
                $cdir = $m2->getParentDir($tid, 'cdir');
            }
        }*/

        //获取内容模板开始
        $cdir = $archivesArr['cdir'];
        if (empty($cdir) || !is_file($cdir)) {
            $cdir = $arctypeArr['cdir'];
            if (empty($cdir) || !is_file($cdir)) {
                $cdir = $m2->getDir($tid, $archivesArr['channel'], 'cdir');
            }
        }


        if (empty($cdir) || !is_file($cdir)) {
            halt($cdir . "内容模板不存在！");
        }

        //定义seo相关内容
        if (empty($arctypeArr['mtitle'])) {
            $mtitle = $archivesArr['title'] . "-" . C('JL_WEBNAME');
        } else {
            $mtitle = $archivesArr['title'] . "-" . $arctypeArr['mtitle'];
        }

        if (empty($arctypeArr['mdesc'])) {
            $mdesc = C('JL_DESC');
        } else {
            $mdesc = $arctypeArr['mdesc'];
        }

        if (empty($arctypeArr['mkey'])) {
            $mkey = C('JL_KEYWORDS');
        } else {
            $mkey = $arctypeArr['mkey'];
        }

        $this->assign('jl_title', $mtitle);
        $this->assign('jl_desc', $mdesc);
        $this->assign('jl_key', $mkey);
        $this->assign("defaultimg", C('SYS_DEFAULT_IMG'));
        $this->assign('c', $archivesArr);
        $this->assign('arctype', $arctypeArr);
        $this->assign('tid', $tid);
        $this->assign('position', $m2->getRouteLine($tid));
        $this->display($cdir);
    }

    /*
    * 外部模型,更多逻辑代码结合项目编写
    */
    public function _extview($id, $table)
    {
        $m = M($table);
        $arr = $m->where('id=' . $id)->find();
        if (empty($arr)) {
            $this->error("404 页面不存在！");
            return;
        } else {
            $tpl = "";
            switch ($table) {
                case 'faq' :
                    $tpl = "content_jiaoliu";
                    break;
                default :
                    break;
            }

            $this->assign("c", $arr);
            $theme = C('SYS_DEFAULT_THEME');
            $this->display($theme . ":" . $tpl);
        }
    }

    //附件下载 针对 download 模型
    public function download()
    {
        $id = $this->_param("id");
        $m = M("Adddownload");
        $arr = $m->where("id = $id")->find();
        if (empty($arr)) {
            $this->error("下载文件不存在");
            return;
        } else {
            if (isIntranet()) {
                $m->where("id = $id")->setInc('click2');
            } else {
                $m->where("id = $id")->setInc('click');
            }
            $fileUrl = $_SERVER['DOCUMENT_ROOT'] . $arr['file'];
            $fileName = $arr['title'] . "." . extend($arr['file']);
            $data = file_get_contents($fileUrl);
            header("Content-type: application/octet-stream");
            header("Accept-Ranges: bytes");
            header("Accept-Length: " . filesize($fileUrl));
            //判断浏览器版本
            $b_ver = $_SERVER["HTTP_USER_AGENT"];
            if (preg_match("/MSIE/", $b_ver)) {
                header("Content-Disposition: attachment; filename=" . urlencode($fileName));
            } else {
                header("Content-Disposition: attachment; filename=" . $fileName);
            }
            echo $data;
        }
    }

    //显示升级提示页面方法
    public function updatebrowser()
    {
        $this->display("default/update");
    }

    Public function verify()
    {
        import('ORG.Util.Image');
        Image::buildImageVerify(4, 1, 'png', 60, 30);
    }

    public function checkmsg(){
        if (IS_POST) {

            if ($_SESSION['verify'] != md5($_POST['code'])) {
                $this->error('验证码输入错误,请重新输入!');
            }

            $model = M("msg");
            $_POST['channel'] = 5;
            $_POST['siteid'] = 1;
            if( $model->create() !== false)
                if (($id = $model->add()) !== false) {
                    $this->success('留言信息提交成功');
                } else {
                    $this->error('留言信息提交失败，请稍后重试。');
                }

        } else {
            $this->error('404 ,对不起，您访问的页面不存在');
        }
    }

    public function memlogin(){
        if(IS_POST){
            if ($_SESSION['verify'] != md5($_POST['code'])) {
                $this->error('验证码输入错误,请重新输入!');
            }

            $userid = $_POST["userid"];
            $pwd = strrev(MD5($_POST['pwd']));
            $model = M("member");

            $rs = $model->where("userid='{$userid}' AND pwd='{$pwd}'")->find();
            if($rs !== false){
                $_SESSION['teacher'] = $rs['userid'];
                $this->success('登陆成功',U('Index/teacher'));
            }else{
                $this->error('登陆失败,请检查您的用户名和密码是否正确');
            }
        }else {
            $this->error('404 ,对不起，您访问的页面不存在');
        }
    }

}
?>